package
{
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	
	/**
	 * ...
	 * @author Pedro Passos
	 */
	public class Level extends MovieClip
	{
		public static var AR_tiros:Array;
		public static var AR_tirosInimigo:Array;
		public static var AR_Inimigos:Array;
		
		private var level:Level;
		
		protected var fundo:Fundo;
		private var inimigo:Inimigo;
		private var delayInimigo:int;
		
		private var contaInimigos:int;
		var heroi:Heroi;
		
		private var currentLevel:int;
		var score:int;
		var scoreFinal:int;
		var hud:MovieClip;
		
		public function Level()
		{
			Main.CleanScreen();
			AR_tiros = new Array()
			AR_tirosInimigo = new Array();
			AR_Inimigos = new Array();
			fundo = null;
			inimigo = null;
			heroi = null;
		
		}
		
		public function setLevel(i:int):void
		{
			Main.CleanScreen();
			Main.stage.addEventListener(Event.ENTER_FRAME, update);
			this.currentLevel = i;
			Instaciar();
		}
		
		private function removeHeroi():void
		{
			if (heroi != null)
			{
				
				if (Main.screen.contains(heroi))
				{
					Main.screen.removeChild(heroi);
					Main.screen.removeEventListener(KeyboardEvent.KEY_DOWN, heroi.Press);
					Main.screen.removeEventListener(KeyboardEvent.KEY_UP, heroi.Release);
					heroi = null;
				}
			}
		}
		
		private function Instaciar():void
		{
			switch (currentLevel)
			{
				case 1: 
					removeHeroi();
					level = new Level1();
					hud = new Hud();
					hud.x = 150;
					hud.y = 130;
					Main.screen.addChild(hud);
					AR_tiros = new Array()
					AR_tirosInimigo = new Array();
					AR_Inimigos = new Array();
					heroi = new Heroi();
					heroi.x = Main.stage.stageWidth / 2;
					heroi.y = Main.stage.stageHeight * 0.8;
					Main.screen.addChild(heroi);
					delayInimigo = 40;
					scoreFinal = 20;
					score = 0;
					trace("criei level 1 " + AR_Inimigos.length + " " + AR_tiros.length + " " + AR_tirosInimigo.length);
					
					break;
				case 2: 
					removeHeroi();
					level = new Level2();
					hud = new Hud();
					hud.x = 120;
					hud.y = 120;
					Main.screen.addChild(hud);
					AR_tiros = new Array()
					AR_tirosInimigo = new Array();
					AR_Inimigos = new Array();
					heroi = new Heroi();
					heroi.x = Main.stage.stageWidth / 2;
					heroi.y = Main.stage.stageHeight * 0.8;
					Main.screen.addChild(heroi);
					delayInimigo = 30;
					scoreFinal = 2;
					score = 0;
					trace("criei level 2 " + AR_Inimigos.length + " " + AR_tiros.length + " " + AR_tirosInimigo.length);
					
					break;
				case 3: 
					break;
				case 4: 
					break;
				case 5: 
					break;
			}
		
		}
		
		public function CriarInimigo(level:int):void
		{
			contaInimigos++;
			if (contaInimigos % delayInimigo == 0)
			{
				switch (level)
				{
					case 1: 
						inimigo = new InimigoFase1();
						break;
					case 2: 
						inimigo = new InimigoFase2();
						break;
				}
				
				inimigo.x = Math.ceil(Math.random() * 750 - 50);
				
				inimigo.y = -20;
				Main.screen.addChild(inimigo);
				Level.AR_Inimigos.push(inimigo);
				contaInimigos = 0;
				
			}
		}
		
		private function PasseiLevel():void
		{
			Main.stage.removeEventListener(Event.ENTER_FRAME, update);
			this.currentLevel++;
			this.level = null;
			this.level = new Level();
			this.level.setLevel(this.currentLevel);
			k = 0;
		
		}
		var k:int
		
		public function update(e:Event):void
		{
			k++
			trace("vou parar : " + k);
			
			if (heroi != null)
			{
				heroi.update();
				CriarInimigo(currentLevel);
				for each (inimigo in AR_Inimigos)
				{
					if (inimigo != null)
					{
						
						inimigo.update();
						inimigo.criaTiro();
					}
				}
				if (level != null)
				{
					level.fundo.update();
				}
				VerificaColisao();
				if (score >= scoreFinal)
				{
					this.PasseiLevel();
				}
				for each (var tiroh:TiroHeroi in Level.AR_tiros)
				{
					if (tiroh != null)
					{
						tiroh.update();
					}
				}
				for each (var tiroi:TiroInimigo in Level.AR_tirosInimigo)
				{
					if (tiroi != null)
					{
						tiroi.update();
					}
				}
			}
			if (hud != null)
			{
				if (vida >= 30)
				{
					hud.gotoAndStop("cheia");
				}
				else if (vida < 30 && vida > 20)
				{
					hud.gotoAndStop("metade");
					
				}
				else if (vida <= 20 && vida > 10)
				{
					hud.gotoAndStop("ultimo");
				}
				else if (vida <= 10 && vida > 0)
				{
					hud.gotoAndStop("fim");
				}
				else
				{
					GameOover();
				}
			}
			trace(vida + "ei olha a vida");
		}
		var vida:int = 30;
		
		private function GameOover():void
		{
			Main.stage.removeEventListener(Event.ENTER_FRAME, update);
			removeHeroi();
			
			Main.CleanScreen();
			var go:GameOver = new GameOver();
			go.x = Main.stage.stageWidth / 2
			go.y = Main.stage.stageHeight / 2;
			Main.screen.addChild(go);
		}
		
		private function VerificaColisao()
		{
			for each (inimigo in AR_Inimigos)
			{
				if (inimigo)
				{
					if (heroi.hitTestObject(inimigo))
					{
						vida--;
					}
				}
			}
			for each (var tiro:TiroHeroi in AR_tiros)
			{
				for each (inimigo in AR_Inimigos)
				{
					if (tiro != null && inimigo != null)
					{
						if (tiro.hitTestObject(inimigo))
						{
							if (Main.screen.contains(inimigo))
							{
								var explosao:MovieClip = new Explosao();
								explosao.x = inimigo.x;
								explosao.y = inimigo.y;
								Main.screen.addChild(explosao);
								Main.screen.removeChild(inimigo);
								AR_Inimigos.splice(inimigo, 1);
								score++;
								
							}
							if (Main.screen.contains(tiro))
							{
								Main.screen.removeChild(tiro);
								AR_tiros.splice(tiro, 1);
							}
							
						}
					}
				}
			}
			for each (var tiroi:TiroInimigo in AR_tirosInimigo)
			{
				if (tiroi != null && heroi != null)
				{
					if (tiroi.hitTestObject(heroi))
					{
						vida--;
					}
				}
			}
		}
	}

}